/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.rest;

import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import me.zhengjie.annotation.Log;
import me.zhengjie.business.domain.Pgjl;
import me.zhengjie.business.service.PgjlService;
import me.zhengjie.business.service.dto.PgjlQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author xiahongbo
* @date 2025-04-28
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "评估结论管理")
@RequestMapping("/api/pgjl")
public class PgjlController {

    private final PgjlService pgjlService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('pgjl:list')")
    public void exportPgjl(HttpServletResponse response, PgjlQueryCriteria criteria) throws IOException {
        pgjlService.download(pgjlService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询评估结论")
    @ApiOperation("查询评估结论")
    @PreAuthorize("@el.check('pgjl:list')")
    public ResponseEntity<Object> queryPgjl(PgjlQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(pgjlService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增评估结论")
    @ApiOperation("新增评估结论")
    @PreAuthorize("@el.check('pgjl:add')")
    public ResponseEntity<Object> createPgjl(@Validated @RequestBody Pgjl resources){
        return new ResponseEntity<>(pgjlService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改评估结论")
    @ApiOperation("修改评估结论")
    @PreAuthorize("@el.check('pgjl:edit')")
    public ResponseEntity<Object> updatePgjl(@Validated @RequestBody Pgjl resources){
        pgjlService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除评估结论")
    @ApiOperation("删除评估结论")
    @PreAuthorize("@el.check('pgjl:del')")
    public ResponseEntity<Object> deletePgjl(@RequestBody String[] ids) {
        pgjlService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/pgjlDownload")
    public ResponseEntity<Resource> downloadLatestFile(String bisPgjlId) throws IOException {
        File file = pgjlService.getLatestGeneratedFile(bisPgjlId);
        if (file == null || !file.exists()) {
            return ResponseEntity.notFound().build();
        }

        Resource resource = new UrlResource(file.toURI());

        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"")
                .body(resource);
    }
}